 package handling.world.messenger;

 import client.MapleCharacter;
 import com.alibaba.druid.pool.DruidPooledConnection;
 import database.DatabaseConnection;
 import handling.world.WorldFindService;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
 import java.sql.SQLException;
 import org.apache.logging.log4j.LogManager;
 import org.apache.logging.log4j.Logger;
 import server.Timer;
 import tools.StringUtil;









 public class MessengerRankingWorker
 {
/*  25 */   private static final Logger log = LogManager.getLogger(MessengerRankingWorker.class.getName());
   private final MapleCharacter[] rankingPlayer;
   private final int[] rankingLove;
   private final long[] lastUpdateTime;

   private MessengerRankingWorker() {
/*  31 */     log.info("正在启动[MessengerRankingWorker]");
/*  32 */     this.rankingPlayer = new MapleCharacter[2]; int i;
/*  33 */     for (i = 0; i < this.rankingPlayer.length; i++) {
/*  34 */       this.rankingPlayer[i] = null;
     }
/*  36 */     this.rankingLove = new int[2];
/*  37 */     for (i = 0; i < this.rankingLove.length; i++) {
/*  38 */       this.rankingLove[i] = 0;
     }
/*  40 */     this.lastUpdateTime = new long[2];
/*  41 */     for (i = 0; i < this.lastUpdateTime.length; i++) {
/*  42 */       this.lastUpdateTime[i] = 0L;
     }
/*  44 */     Timer.WorldTimer.getInstance().register(this::updateRankFromDB, 10800000L, 0L);
   }

   public static MessengerRankingWorker getInstance() {
/*  48 */     return SingletonHolder.instance;
   }

   public void updateRankFromDB() {
/*  52 */     String malesql = "SELECT chr.id, chr.gender, chr.love FROM characters AS chr LEFT JOIN accounts AS acc ON chr.accountid = acc.id WHERE chr.gm = 0 AND chr.gender = 0 AND acc.banned = 0 AND chr.love > 0 ORDER BY chr.love LIMIT 1";
/*  53 */     String femalesql = "SELECT chr.id, chr.gender, chr.love FROM characters AS chr LEFT JOIN accounts AS acc ON chr.accountid = acc.id WHERE chr.gm = 0 AND chr.gender = 1 AND acc.banned = 0 AND chr.love > 0 ORDER BY chr.love LIMIT 1";
/*  54 */     try (DruidPooledConnection con = DatabaseConnection.getInstance().getConnection()) {



/*  58 */       PreparedStatement ps = con.prepareStatement(malesql);
/*  59 */       ResultSet rs = ps.executeQuery();
/*  60 */       int maleId = 0, malelove = 0;
/*  61 */       if (rs.next()) {
/*  62 */         maleId = rs.getInt("id");
/*  63 */         malelove = rs.getInt("love");
       }
/*  65 */       rs.close();
/*  66 */       ps.close();
/*  67 */       MapleCharacter malechr = null;
/*  68 */       if (maleId > 0) {
/*  69 */         malechr = WorldFindService.getInstance().findCharacterById(maleId);
/*  70 */         if (malechr == null) {
/*  71 */           malechr = MapleCharacter.loadCharFromDB(maleId, null, false, null);
         }
       }
/*  74 */       this.rankingPlayer[0] = malechr;
/*  75 */       this.rankingLove[0] = malelove;
/*  76 */       this.lastUpdateTime[0] = System.currentTimeMillis();
/*  77 */       if (maleId > 0) {
/*  78 */         log.info("更新聊天招待人气排行榜 男角色 - Id: " + StringUtil.getRightPaddedStr(String.valueOf(maleId), ' ', 6) + " 好感度: " + StringUtil.getRightPaddedStr(String.valueOf(malelove), ' ', 4) + " 名字: " + ((malechr != null) ? malechr.getName() : "????"));
       } else {
/*  80 */         log.info("更新聊天招待人气排行榜 男角色 - 暂无信息... ");
       }



/*  85 */       ps = con.prepareStatement(femalesql);
/*  86 */       rs = ps.executeQuery();
/*  87 */       int femaleId = 0, femalelove = 0;
/*  88 */       if (rs.next()) {
/*  89 */         femaleId = rs.getInt("id");
/*  90 */         femalelove = rs.getInt("love");
       }
/*  92 */       rs.close();
/*  93 */       ps.close();
/*  94 */       MapleCharacter femalechr = null;
/*  95 */       if (femaleId > 0) {
/*  96 */         femalechr = WorldFindService.getInstance().findCharacterById(femaleId);
/*  97 */         if (femalechr == null) {
/*  98 */           femalechr = MapleCharacter.loadCharFromDB(femaleId, null, false, null);
         }
       }
/* 101 */       this.rankingPlayer[1] = femalechr;
/* 102 */       this.rankingLove[1] = femalelove;
/* 103 */       this.lastUpdateTime[1] = System.currentTimeMillis();
/* 104 */       if (femaleId > 0) {
/* 105 */         log.info("更新聊天招待人气排行榜 女角色 - Id: " + StringUtil.getRightPaddedStr(String.valueOf(femaleId), ' ', 6) + " 好感度: " + StringUtil.getRightPaddedStr(String.valueOf(femalelove), ' ', 4) + " 名字: " + ((femalechr != null) ? femalechr.getName() : "????"));
       } else {
/* 107 */         log.info("更新聊天招待人气排行榜 女角色 - 暂无信息... ");
       }
/* 109 */     } catch (SQLException se) {
/* 110 */       log.error("更新聊天招待人气排行榜失败..", se);
     }
   }

   public void updateRankFromPlayer(MapleCharacter chr) {
/* 115 */     if (chr == null || chr.isGM()) {
       return;
     }
/* 118 */     int num = chr.getGender();
/* 119 */     if (chr.getLove() > this.rankingLove[num]) {
/* 120 */       this.rankingPlayer[num] = chr;
/* 121 */       this.rankingLove[num] = chr.getLove();
/* 122 */       this.lastUpdateTime[num] = System.currentTimeMillis();
     }
   }

   public MapleCharacter getRankingPlayer(int num) {
/* 127 */     return this.rankingPlayer[num];
   }

   public int getRankingLove(int num) {
/* 131 */     return this.rankingLove[num];
   }

   public long getLastUpdateTime(int num) {
/* 135 */     return this.lastUpdateTime[num];
   }

   public void resetLastUpdateTime(int num) {
/* 139 */     this.lastUpdateTime[num] = System.currentTimeMillis();
   }

   private static class SingletonHolder
   {
/* 144 */     protected static final MessengerRankingWorker instance = new MessengerRankingWorker();
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\world\messenger\MessengerRankingWorker.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */